﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace Battle.View
{
    public class Projectile : MonoBehaviour, IBattleInst
    {
        /// <summary>
        /// 抛射物数据
        /// </summary>
        protected ProjectileData projResData;
        /// <summary>
        /// 抛射物的创造者
        /// </summary>
        protected BattleCharacter creator;
        /// <summary>
        /// 哪个技能创造的
        /// </summary>
        protected BattleSkill creatorSkill;
        /// <summary>
        /// 技能Release创造
        /// </summary>
        protected BattleSkillRelease creatorSkillRelease;
        /// <summary>
        /// 抛射物打击坐标
        /// </summary>
        protected BattlePos targetPos;

        protected GameObject proj;
        protected bool isLoaded = false;

        public bool isNeedRecycle = false;

        public void Init(object data)
        {
            isLoaded = false;
            proj = null;
            isNeedRecycle = false;
            ProjectilePacket pp = data as ProjectilePacket;
            if (pp != null)
            {
                projResData = pp.projectileData;
                creator = BattleViewCtrl.GetInstance().GetBattleCharacter(pp.sourceUid);
                creatorSkill = creator.GetSkill(pp.skillType);
                creatorSkillRelease = creatorSkill.GetSkillRelease(pp.releaseId);
                targetPos = new BattlePos(pp.targetFPId, pp.targetCamp);
            }
            else
            {
                Destroy(gameObject);
            }
        }
        public void Load()
        {
            proj = ParticleFactory.SpawnParticle(projResData.resUrl);
            if (proj != null)
            {
                proj.transform.SetParent(transform);
                proj.transform.localPosition = Vector3.zero;
                proj.transform.localScale = Vector3.one;
                proj.transform.localEulerAngles = Vector3.zero;
                isLoaded = true;
            }
            else
            {
                Debug.LogError("load projectile not exist. " + projResData.resUrl);
                Destroy(gameObject);
            }
        }
        public void Unload()
        {
            if (isLoaded)
            {
                ParticleFactory.UnSpawnParticle(proj);
                proj = null;
                isLoaded = false;
            }
        }
        public void Run()
        {
            Shoot();
        }
        public void Tick()
        {
            Tick0();
        }

        public void Stop()
        {
            Unload();
            BattleViewCtrl.GetInstance().UnSpawnProjectile(this);
            GameObject.Destroy(gameObject);
        }

        protected virtual void Shoot()
        {

        }

        protected virtual void Tick0()
        {

        }

        protected virtual void OnCollider()
        {
        }
    }
}
